package com.freewater.springboot.demo.aop.advice.around;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class PersonAspect {

    @Pointcut("execution(* com.freewater.springboot.demo.aop.advice.around.PersonService.*(..))")
    public void allMethod() {

    }

    // 注意：It is therefore recommended that an around advice method declare a return type of Object
    // 注意：@Around类型的advice，第一个参数必须是ProceedingJoinPoint类型的（仅有@Around类型的advice有这个强制要求）
    @Around(value = "allMethod() && args(name,age)", argNames = "joinPoint,age,name")
    public Object beforeAdvice(ProceedingJoinPoint joinPoint, int age, String name) {
        System.out.println("params age: " + age);
        System.out.println("params name: " + name);
        try {
            final Person person = (Person) joinPoint.proceed(); // 空参数的话，spring默认会把相应的参数传递给实际的方法
            //final Person person = (Person) joinPoint.proceed(new Object[]{name, age});
            System.out.println("return person: " + person);
            return person;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
